<?php
/*
Plugin Name: reload act file
Plugin URI: http://www.sang.com/
Description: ACTION类代码热更新
Version: 1.0.0
Author: Sang
Author URI: http://www.sang.com/
License: GPLv2 or later
Text Domain: Sang
*/
// add_action('app_start',array('AppStart','init'));
class AppStart{
	private static $recofiles;
	public static function init($act_file){
        //用inotify监控目录变化，重载来替代runkit
        return;
        if(defined('DEBUG')){
            if(!extension_loaded('runkit')){
                throw new \Exception("The lib [runkit] not installed",205);
            }
            self::recoreFileInfo($act_file);
            //重载代码，依赖runkit库
            self::reloadAct($act_file);
        }
	}

	public static function recoreFileInfo($file){
        if(!isset(self::$recofiles[$file]))
            self::$recofiles[$file] = time();
    }

    public static function reloadAct($act_file){
        clearstatcache();
        $fstat = stat($act_file);
        //修改时间大于加载时的时间
        if($fstat['mtime'] > self::$recofiles[$act_file])
        {
            runkit_import($act_file, RUNKIT_IMPORT_CLASS_METHODS|RUNKIT_IMPORT_OVERRIDE);
            self::$recofiles[$act_file] = time();
        }
    }
}